package com.jy.admin.rest.response;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/11/10
 * Time: 11:28 上午
 */
public class ProductListResponse implements Serializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductListResponse.class);

    private static final long serialVersionUID = -5541725864725732951L;

    private List<Item> list = new ArrayList<>();

    public ProductListResponse(List<ProductCategory> productCategories, Integer forGp) {

        if (!CollectionUtils.isEmpty(productCategories)) {
            productCategories.forEach(productCategory -> {
                LOGGER.info("==  查询列表返回结果 ==,gp1:{},gp2:{}", productCategory.getForGp(), forGp);
                if (productCategory.getForGp().equals(forGp) || productCategory.getForGp().equals(3)) {
                    Item item = new Item();
                    item.setCode(productCategory.getCode())
                            .setSelling(productCategory.getIsSelling())
                            .setLogo(productCategory.getLogo())
                            .setPosition(productCategory.getCategoryType())
                            .setApplyPercent(productCategory.getApplyRate())
                            .setName(productCategory.getName())
                            .setDisplayNo(productCategory.getSort())
                            .setDayLimit(productCategory.getDailyApplyLimit())
                            .setForGp(productCategory.getForGp());

                    //全部 1 新客 2 老客 3 全部 4 停止放款
                    Integer lendingUserType = 3;

                    //0 没有 1新品 2热品
                    Integer promotionTagType = 0;

                    //是否已满 0 未满 1 满
                    Integer fullType = 0;

                    //0 api  1:GP
                    Integer jumpType = 0;
                    String jumpUrl = "";

                    String extraInfo = productCategory.getExtraInfo();
                    if (!StringUtils.isEmpty(extraInfo)) {
                        Any deserialize = JsonIterator.deserialize(extraInfo);
                        Any lendingUserType1 = deserialize.get("lendingUserType");
                        if (lendingUserType1 != null) {
                            lendingUserType = lendingUserType1.toInt();
                        }

                        Any promotionTagType1 = deserialize.get("promotionTagType");
                        if (promotionTagType1 != null) {
                            promotionTagType = promotionTagType1.toInt();
                        }
                        Any fullType1 = deserialize.get("fullType");
                        if (fullType1 != null) {
                            fullType = fullType1.toInt();
                        }

                        Any jumpType1 = deserialize.get("jumpType");
                        if (jumpType1 != null) {
                            jumpType = jumpType1.toInt();
                        }

                        Any jumpUrl1 = deserialize.get("jumpUrl");
                        if (jumpUrl1 != null) {
                            jumpUrl = jumpUrl1.toString();
                        }
                    }

                    item.setLendingUserType(lendingUserType);
                    item.setPromotionTagType(promotionTagType);
                    item.setFullType(fullType).setJumpType(jumpType).setJumpUrl(jumpUrl);

                    list.add(item);
                }
            });
        }

    }

    public List<Item> getList() {
        return list;
    }

    public ProductListResponse setList(List<Item> list) {
        this.list = list;
        return this;
    }

    private class Item {
        private String logo;
        private String name;
        private String code;

        //0 api  1:GP
        Integer jumpType = 0;
        String jumpUrl = "";

        //全部 1 新客 2 老客 3 全部 4 停止放款
        private Integer lendingUserType;

        //0 没有 1新品 2热品
        private Integer promotionTagType;

        //是否已满 0 未满 1 满
        private Integer fullType;

        private Integer displayNo;

        private Integer applyPercent;

        private Integer selling;

        private Integer position;

        private Integer dayLimit;

        private Integer forGp;

        public Integer getJumpType() {
            return jumpType;
        }

        public Item setJumpType(Integer jumpType) {
            this.jumpType = jumpType;
            return this;
        }

        public String getJumpUrl() {
            return jumpUrl;
        }

        public Item setJumpUrl(String jumpUrl) {
            this.jumpUrl = jumpUrl;
            return this;
        }

        public Integer getPromotionTagType() {
            return promotionTagType;
        }

        public Item setPromotionTagType(Integer promotionTagType) {
            this.promotionTagType = promotionTagType;
            return this;
        }

        public Integer getFullType() {
            return fullType;
        }

        public Item setFullType(Integer fullType) {
            this.fullType = fullType;
            return this;
        }

        public Integer getForGp() {
            return forGp;
        }

        public Item setForGp(Integer forGp) {
            this.forGp = forGp;
            return this;
        }

        public Integer getLendingUserType() {
            return lendingUserType;
        }

        public Item setLendingUserType(Integer lendingUserType) {
            this.lendingUserType = lendingUserType;
            return this;
        }

        public Integer getDayLimit() {
            return dayLimit;
        }

        public Item setDayLimit(Integer dayLimit) {
            this.dayLimit = dayLimit;
            return this;
        }

        public String getName() {
            return name;
        }

        public Item setName(String name) {
            this.name = name;
            return this;
        }

        public String getLogo() {
            return logo;
        }

        public Item setLogo(String logo) {
            this.logo = logo;
            return this;
        }

        public String getCode() {
            return code;
        }

        public Item setCode(String code) {
            this.code = code;
            return this;
        }

        public Integer getDisplayNo() {
            return displayNo;
        }

        public Item setDisplayNo(Integer displayNo) {
            this.displayNo = displayNo;
            return this;
        }

        public Integer getApplyPercent() {
            return applyPercent;
        }

        public Item setApplyPercent(Integer applyPercent) {
            this.applyPercent = applyPercent;
            return this;
        }

        public Integer getSelling() {
            return selling;
        }

        public Item setSelling(Integer selling) {
            this.selling = selling;
            return this;
        }

        public Integer getPosition() {
            return position;
        }

        public Item setPosition(Integer position) {
            this.position = position;
            return this;
        }
    }
}
